//
//  HWRequestLogManage
//
//  Created by 李含文 on 2021/4/28.
//  Copyright © 2021 MINISO. All rights reserved.
//

#import "HWLogViewController.h"
#import "HWLogCell.h"
#import "HWLogContentViewController.h"

@interface HWLogViewController ()<UITableViewDelegate, UITableViewDataSource>

/// <#name#>
@property (nonatomic, strong) UITableView *tabeleView;

/// <#name#>
@property (nonatomic, strong) NSMutableArray<HWLogModel *> *itemArray;

@end

@implementation HWLogViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self addObserver];
    [self setUI];
    [self loadData];
}

- (void)addObserver {
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(loadData) name:k_HWLogViewModel_DataUpdate object:nil];
}
- (void)setUI {
    self.view.backgroundColor = UIColor.whiteColor;
    self.title = @"请求日志";
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"退出" style:(UIBarButtonItemStylePlain) target:self action:@selector(leftBarButtonClick)];
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"清理" style:(UIBarButtonItemStylePlain) target:self action:@selector(rightBarButtonClick)];
    
    [self.view addSubview:self.tabeleView];
}

- (void)leftBarButtonClick {
    [self dismissViewControllerAnimated:YES completion:nil];
}
- (void)rightBarButtonClick {
    [[HWLogViewModel shared] cleanData];
}

- (void)loadData {
    self.itemArray = [HWLogViewModel shared].models;
    [self.tabeleView reloadData];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.itemArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    HWLogCell *cell = [tableView dequeueReusableCellWithIdentifier:@"HWLogCellID" forIndexPath:indexPath];
    HWLogModel *model = self.itemArray[indexPath.row];
    cell.model = model;
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    HWLogModel *model = self.itemArray[indexPath.row];
    HWLogContentViewController *vc = [HWLogContentViewController new];
    vc.model = model;
    [self.navigationController pushViewController:vc animated:YES];
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    HWLogModel *model = self.itemArray[indexPath.row];
    return model.cellH;
}

- (UITableView *)tabeleView {
    if(!_tabeleView) {
        float statusBarHeight = 20;
        if (@available(iOS 13.0, *)) {
            UIStatusBarManager *statusBarManager = [UIApplication sharedApplication].windows.firstObject.windowScene.statusBarManager;
            statusBarHeight = statusBarManager.statusBarFrame.size.height;
        } else {
            statusBarHeight = [UIApplication sharedApplication].statusBarFrame.size.height;
        }
        _tabeleView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, UIScreen.mainScreen.bounds.size.width, UIScreen.mainScreen.bounds.size.height-(statusBarHeight+44))];
        _tabeleView.delegate = self;
        _tabeleView.rowHeight = 115;
        _tabeleView.dataSource = self;
        [_tabeleView registerNib:[UINib nibWithNibName:@"HWLogCell" bundle:nil] forCellReuseIdentifier:@"HWLogCellID"];
        _tabeleView.tableFooterView = [UIView new];
    }
    return _tabeleView;
}

- (NSMutableArray<HWLogModel *> *)itemArray {
    if (!_itemArray)
        _itemArray = [NSMutableArray array];
    return _itemArray;
}

- (void)dealloc {
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}
@end
